﻿using System;

namespace Devme.Diagnostics
{
    public sealed class StopwatchResult
    {
        public TimeSpan Current { get; private set; }
        public TimeSpan Total { get; private set; }
        public uint Count { get; private set; }
        public TimeSpan Min { get; private set; }
        public TimeSpan Max { get; private set; }
		public TimeSpan Avg { get; private set; }

    	public StopwatchResult(TimeSpan current, TimeSpan total, uint count, TimeSpan min, TimeSpan max)
        {
            Current = current;
            Total = total;
            Count = count;
            Min = min;
            Max = max;
			Avg = TimeSpan.FromTicks(Total.Ticks / Count);
        }

        public override string ToString()
        {
            return string.Format("{{current:'{0}', total:'{1}', count:'{2}', min:'{3}', max:'{4}', avg:'{5}'}}", Current, Total, Count, Min, Max, Avg);
        }

        public bool Equals(StopwatchResult other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
        	return other.Current.Equals(Current)
        	       && other.Total.Equals(Total)
        	       && other.Count == Count
        	       && other.Min.Equals(Min)
        	       && other.Max.Equals(Max)
        	       && other.Avg.Equals(Avg);
        }

        public override bool Equals(object other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            if (other.GetType() != typeof(StopwatchResult))
            {
                return false;
            }
            return Equals((StopwatchResult)other);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = Current.GetHashCode();
                result = (result * 397) ^ Total.GetHashCode();
                result = (result * 397) ^ Count.GetHashCode();
                result = (result * 397) ^ Min.GetHashCode();
                result = (result * 397) ^ Max.GetHashCode();
				result = (result * 397) ^ Avg.GetHashCode();
                return result;
            }
        }
    }
}